package com.yami.shop.distribution.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.distribution.comment.dto.DistributionUserIncomeDto;
import com.yami.shop.distribution.comment.dto.DistributionUserIncomeOrderDto;
import com.yami.shop.distribution.comment.dto.StatisticsDisUserIncomeDto;
import com.yami.shop.distribution.comment.model.DistributionUser;
import com.yami.shop.distribution.comment.model.DistributionUserIncome;
import com.yami.shop.distribution.comment.service.DistributionUserIncomeService;
import com.yami.shop.distribution.comment.service.DistributionUserService;
import com.yami.shop.security.api.util.SecurityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author yami
 */
@Api(tags = "分销员收入接口")
@AllArgsConstructor
@RestController
@RequestMapping("/p/distribution/income")
public class DistributionUserIncomeController {
    final private DistributionUserIncomeService distributionUserIncomeService;
    final private DistributionUserService distributionUserService;

    /**
     * 统计金额
     */
    @GetMapping("/statistics")
    @ApiOperation(value = "统计分销员今日、本月的收益", notes = "")
    public ResponseEntity<StatisticsDisUserIncomeDto> statisticsDistributionUserIncome() {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        String userId = SecurityUtils.getUser().getUserId();
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, shopId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是本店的分销员
            throw new YamiShopBindException("yami.distribution.user.no.shop");
        }
        return ResponseEntity.ok(distributionUserIncomeService.statisticsDistributionUserIncome(distributionUser.getDistributionUserId()));
    }

    @GetMapping("/page")
    @ApiOperation(value = "分页获取收益列表", notes = "")
    public ResponseEntity<IPage<DistributionUserIncomeDto>> getDistributionUserIncomePage(PageParam<DistributionUserIncome> page) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        String userId = SecurityUtils.getUser().getUserId();
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(userId, shopId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是本店的分销员
            throw new YamiShopBindException("yami.distribution.user.no.shop");
        }
        IPage<DistributionUserIncomeDto> distributionUserIncomeDto = distributionUserIncomeService.getDistributionUserIncomePage(page, distributionUser.getDistributionUserId());
        return ResponseEntity.ok(distributionUserIncomeDto);
    }

    @GetMapping("/myPromotionOrder")
    @ApiOperation(value = "我的推广订单", notes = "通过状态获取我的推广订单(0:待支付 1:待结算 2:已结算 -1:订单失效)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "state", value = "状态", dataType = "Long", example = "1")
    })
    public ResponseEntity<IPage<DistributionUserIncomeOrderDto>> getMyPromotionOrderByState(PageParam<DistributionUserIncomeOrderDto> page, Integer state) {
        Long shopId = Constant.PLATFORM_SHOP_ID;
        DistributionUser distributionUser = distributionUserService.getByUserIdAndShopId(SecurityUtils.getUser().getUserId(), shopId);
        if (Objects.isNull(distributionUser)) {
            // 您还不是本店的分销员
            throw new YamiShopBindException("yami.distribution.user.no.shop");
        }
        IPage<DistributionUserIncomeOrderDto> result = distributionUserIncomeService.getMyPromotionOrderByState(page, distributionUser.getDistributionUserId(), state);
        return ResponseEntity.ok(result);
    }

}
